#include "Building.h"
#include "EntityTypes.h"
#include "GLPrimitives.h"
#include "GameWorld.h"
#include "Globals.hpp"
#include "General Utils.hpp"

Building::Building(string name, Vector2D pos, int type):BaseGameEntity(pos, name, type)
{
	switch(type)
	{
	case HutID:
		SetBRadius(75.0);
		break;
	default:
		assert(0 && "INVALID ENTITY ID, DAWG!");
	}


	m_iMaxHealth = 5000;
	m_iCurrHealth = 0;
	m_iState = Unbuilt;


	// The drawing rect must be set so the building's position is at it's center
	Rect.x = Pos().x - Rect.w/2;
	Rect.y = Pos().y - Rect.h/2;

	
	GameRectangle TempRect(Rect.x, Rect.y, Rect.w, Rect.h);
	World->GetNavGraph()->SetEdgesToBuilding(TempRect, World->GetMap()->MapWidth());

}

void Building::Repair() 
{
	m_iCurrHealth += 50;

	

	if(m_iCurrHealth > m_iMaxHealth)
		m_iCurrHealth = m_iMaxHealth;

	if(m_iCurrHealth == m_iMaxHealth)
		m_iState = Built;
}


void Building::Update(double time_elapsed)
{
	BaseGameEntity::Update(time_elapsed);

	if(m_iState == Built)
	{
		if(m_iCurrHealth < m_iMaxHealth)
			m_iState = Damaged;
	}


}

// ======================== Render =========================
// The render function merely calls apply surface and
// draws a health bar beneath the building

void Building::Render()
{
	BaseGameEntity::Render();
	DrawHealthBar();
}

// ==================== Draw Health Bar =====================
// Draws a rectangle proportional to the fraction of the 
// current health the building has.

void Building::DrawHealthBar()
{
	// Draw the background rectangle
	DrawPrimitives->DrawRectangle(Rect.x - ClipPlane->GetX(), Rect.y + Rect.h + 10 - ClipPlane->GetY(), Rect.x + Rect.w - ClipPlane->GetX(), Rect.y + Rect.h + 20 - ClipPlane->GetY(), red);

	// The fraction of the health the building has
	double FractHealth = (double)m_iCurrHealth / m_iMaxHealth;

	// Draws the actual health level
	DrawPrimitives->DrawRectangle(Rect.x - ClipPlane->GetX(), Rect.y + Rect.h + 10 - ClipPlane->GetY(), Rect.x + Rect.w*FractHealth - ClipPlane->GetX(), Rect.y + Rect.h + 20 - ClipPlane->GetY(), white);
}